package bwfs

//1.基础词频统计:Basic word frequency statistics

import (
	"fmt"
	"io/ioutil"
	"regexp"
	"sort"
	"strings"
)

// WordFreqMap 单词频率映射
type WordFreqMap map[string]int

// ReadFile 读取文件内容
func ReadFile(filePath string) (string, error) {
	content, err := ioutil.ReadFile(filePath)
	if err != nil {
		return "", err
	}
	return string(content), nil
}

// Tokenize 分词函数
func Tokenize(text string) []string {
	//go语言中的正则表达式应用，1：解析编译，2：提取需要信息
	// 解析、编译正则表达式匹配单词
	re := regexp.MustCompile(`[a-zA-Z0-9]+`)
	// 提取需要信息
	words := re.FindAllString(text, -1)

	// 统一将单词转换为小写
	for i, word := range words {
		words[i] = strings.ToLower(word)
	}
	return words
}

// CountWordFreq 统计单词频率
func CountWordFreq(words []string) WordFreqMap {
	freqMap := make(WordFreqMap)
	for _, word := range words {
		freqMap[word]++
	}
	return freqMap
}

// PrintWordFreq 排序并输出结果
func PrintWordFreq(freqMap WordFreqMap) {
	type kv struct {
		Key   string
		Value int
	}

	var sortedWords []kv
	for word, freq := range freqMap {
		sortedWords = append(sortedWords, kv{word, freq})
	}

	// 按照频率排序
	sort.Slice(sortedWords, func(i, j int) bool {
		return sortedWords[i].Value > sortedWords[j].Value
	})

	// 输出结果
	for _, kv := range sortedWords {
		fmt.Printf("%s: %d\n", kv.Key, kv.Value)
	}
}
